<!-- 采购合同编辑 -->
<template>
  <div class="registerDetail">
    <van-nav-bar
      :title="$route.meta.title"
      left-text="返回"
      left-arrow
      :fixed="true"
      :placeholder="true"
      @click-left="$router.go(-1)"
    />
    <!-- 表单 -->
    <div class="formData">
      <van-form ref="form">
        <van-field
          readonly
          v-model="formData.billNumber"
          name="出库单号"
          label="出库单号"
          input-align="right"
          placeholder="请输入"
        />
        <van-field
          readonly
          v-model="formData.payerName"
          name="销售合同号"
          label="销售合同号"
          input-align="right"
          placeholder="请输入"
        />
        <van-field
          readonly
          v-model="formData.vinCode"
          name="车架号"
          label="车架号"
          input-align="right"
          placeholder="请输入"
        />
        <van-field
          v-model="formData.brandName"
          readonly
          name="所属品牌"
          label="所属品牌"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
        />

        <van-field
          v-model="formData.showOrgName"
          name="展厅/维修中心"
          label="展厅/维修中心"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          readonly
          label-width="130px"
        />
        <van-field
          v-model="formData.bizType"
          readonly
          name="出库类型"
          label="出库类型"
          input-align="right"
          placeholder=""
          right-icon="arrow"
        />
        <van-field
          v-model="formData.brand"
          readonly
          name="出库仓库"
          label="出库仓库"
          input-align="right"
          placeholder=""
          right-icon="arrow"
        />
        <van-field
          v-model="formData.salesType"
          readonly
          name="销售类型"
          label="销售类型"
          input-align="right"
          placeholder=""
          right-icon="arrow"
        />

        <van-field
          v-model="formData.bizDate"
          name="出库日期"
          label="出库日期"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelectTime('bizDate')"
          readonly
        />
        <div class="carInfo">
          <div class="carInfo_title">
            <span style="color: red">*</span>
            销售顾问确认信息
          </div>
          <div class="checkbox">
            <div class="check_item">
              <van-checkbox
                v-model="dataSource.isCheck"
                icon-size="12"
                shape="square"
              >
                整车检查完毕
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isPropertOk"
                icon-size="12"
                shape="square"
              >
                车内配置完好
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isTookOk"
                icon-size="12"
                shape="square"
              >
                随车工具齐全
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isSurfaceOk"
                icon-size="12"
                shape="square"
              >
                外观完好无损
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isPaintColorOk"
                icon-size="12"
                shape="square"
              >
                外观漆面无色差
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isNameplate"
                icon-size="12"
                shape="square"
              >
                名牌(含生产日期)
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isSaleContract"
                icon-size="12"
                shape="square"
              >
                销售合同
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isSupply"
                icon-size="12"
                shape="square"
              >
                补充协议
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isCard"
                icon-size="12"
                shape="square"
              >
                刷卡单
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isAddpart"
                icon-size="12"
                shape="square"
              >
                选装件
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isInsurance"
                icon-size="12"
                shape="square"
              >
                保险
              </van-checkbox>
            </div>
            <div class="check_item">
              <van-checkbox
                v-model="formData.isCarlicense"
                icon-size="12"
                shape="square"
              >
                委托上牌
              </van-checkbox>
            </div>
          </div>
        </div>
        <van-field
          v-if="formData.isSurfaceOk"
          v-model="formData.carLooks"
          name="车辆外观情况"
          label="车辆外观情况"
          input-align="right"
          placeholder="请输入"
          label-width="130px"
          required
          :rules="[{ required: true }]"
        />
        <van-field
          required
          :rules="[{ required: true }]"
          v-if="formData.isSurfaceOk"
          v-model="formData.breakOrLoss"
          name="质损或缺件说明"
          label="质损或缺件说明"
          input-align="right"
          placeholder="请输入"
          label-width="130px"
        />
        <van-field
          required
          v-model="formData.remark"
          name="备注"
          label="备注"
          type="textarea"
          input-align="left"
          placeholder=""
        />
        <van-field
          required
          v-model="formData.otherMatters"
          name="其他事项"
          label="其他事项"
          type="textarea"
          input-align="left"
          placeholder=""
        />

        <!-- 下拉选择弹窗 -->
        <van-popup v-model="showSelectPicker" position="bottom">
          <van-field
            name="模糊查询"
            v-if="likeWhoField == 'signCompanyId'"
            input-align="left"
            v-model="likeWho"
            label="模糊查询"
            placeholder="请输入模糊查询关键词"
          />
          <van-picker
            show-toolbar
            :columns="columns"
            @confirm="handleSelectConfirm"
            @cancel="showSelectPicker = false"
            value-key="name"
          />
        </van-popup>

        <!-- 时间选择弹窗 -->
        <van-popup v-model="showTimerPicker" position="bottom">
          <van-datetime-picker
            v-model="currentDate"
            type="date"
            @confirm="onTimeConfirm($event)"
            @cancel="showTimerPicker = false"
            :formatter="formatter"
          />
        </van-popup>
        <div class="footerBtn">
          <div class="saveBtn" @click="formOnSubmit(1)">保存</div>
          <div class="submitBtn" @click="formOnSubmit(2)">提交</div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  Search,
  Empty,
  Field,
  Form,
  Uploader,
  DatetimePicker,
  Tab,
  Tabs,
  ImagePreview,
  Notify,
} from "vant";
import { getCookies } from "@/utils/util.js";
import sellManageApi from "@/api/sellManageApi";
import commonApi from "@/api/commonApi";
export default {
  name: "PaymentOrderEdit",
  components: {
    Search,
    Empty,
    Field,
    Form,
    Uploader,
    DatetimePicker,
    Tab,
    Tabs,
    ImagePreview,
    Notify,
  },
  data() {
    return {
      showTimerPicker: false,
      formData: {
        billId: "", //销售出库单ID，必填
        bizDate: "", //出库日期
        remark: "", //备注
        otherMatters: "", //其他事项
        isCheck: "", //整车检查完毕，选中为1，否则为0，特定这两个值
        isPropertOk: "", //车内配置完好，选中为1，否则为0，特定这两个值
        isTookOk: "", //随车工具齐全，选中为1，否则为0，特定这两个值
        isSurfaceOk: "", //外观完好无损，选中为1，否则为0，特定这两个值
        isPaintColorOk: "", //外观漆面无色差，选中为1，否则为0，特定这两个值
        isNameplate: "", //铭牌含生产日期，选中为1，否则为0，特定这两个值
        isSaleContract: "", //销售合同，选中为1，否则为0，特定这两个值
        isSupply: "", //补充协议，选中为1，否则为0，特定这两个值
        isCard: "", //刷卡单，选中为1，否则为0，特定这两个值
        isAddpart: "", //选装件，选中为1，否则为0，特定这两个值
        isInsurance: "", //保险，选中为1，否则为0，特定这两个值
        isCarlicense: "", //委托上牌，选中为1，否则为0，特定这两个值
        carLooks: "", //车身外观情况
        breakOrLoss: "", //质损或缺件说明
      },
      dataSource: {
        billId: "", //单据Id
        billNumber: "", //出库单编号
        bizDate: "", //出库日期
        bizType: "", //出库类型
        billStatus: "", //出库状态
        brandName: "", //所属品牌
        showOrgName: "", //展厅\维修中心
        remark: "", //备注
        otherMatters: "", //其他事项
        payerName: "", //销售合同号
        vinCode: "", //车架号
        carMiles: "", //车辆公里数
        key: "", //钥匙数量
        consignmentCar: "", //寄售车辆
        isCheck: "", //整车检查完毕
        isPropertOk: "", //车内配置完好
        isTookOk: "", //随车工具齐全
        isSurfaceOk: "", //外观完好无损
        isPaintColorOk: "", //外观漆面无色差
        isNameplate: "", //铭牌含生产日期
        isSaleContract: "", //销售合同
        isSupply: "", //补充协议
        isCard: "", //刷卡单
        isAddpart: "", //选装件
        isInsurance: "", //保险
        isCarlicense: "", //委托上牌
        carLooks: "", //车身外观情况
        breakOrLoss: "", //质损或缺件说明
        isCarAmt: "", //车款
        isPartAmt: "", //配件款
        isOtherAmt: "", //其他费用
        isPayAmt: "", //代付款证明
      },
      orderId: "",
      billNumber: "",

      columns: [],
      showSelectPicker: false,
      detailId: "",
      userId: getCookies("userId"), //用户ID，必填，String
      isflagList: [
        { name: "是", value: 1 },
        { name: "否", value: 0 },
      ],
      currentDate: new Date(),
      showOrgList: [],
      payTypeList: [],
      showCompanyList: [],
      showbankAccountList: [],
      paymentTypeList: [],
      likeWhoField: "",
      likeWho: "",
    };
  },
  watch: {
    likeWho: function (nval, oval) {
      if (nval) {
        let newArr = this.showCompanyList.filter((item) => {
          return item.name.indexOf(nval) > -1;
        });
        console.log(newArr);
        this.columns = newArr;
      } else {
        this.columns = this.showCompanyList;
      }
    },
  },
  mounted() {
    if (this.$route.query) {
      this.detailId = this.$route.query.billId;
      this.getDetail(this.detailId);
      this.orderId = this.$route.query.orderId;
      this.billNumber = this.$route.query.billNumber;
    }
  },
  methods: {
    getDetail(billId) {
      sellManageApi
        .secondHandCarFacade({
          billId,
          method: "getSaleIssuedDataById",
        })
        .then((res) => {
          if (res.code == 1) {
            for (const key in res.data) {
              if (res.data[key] == null || res.data[key] == undefined) {
                res.data[key] = "";
              }
            }
            this.formData = { ...this.formData, ...res.data };
          }
        });
    },

    // 选择下拉
    handleSelect(field, fieldName) {
      this.showSelectPicker = true;
      this.likeWhoField = field;
      this.field = field;
      this.fieldName = fieldName;
    },
    // 选择下拉确认
    handleSelectConfirm(value, index) {
      if (
        this.field == "showOrgId" ||
        this.field == "signCompanyId" ||
        this.field == "bankAccountId"
      ) {
        this.formData[this.field] = value.billId;
        this.formData[this.fieldName] = value.name;
      } else {
        this.formData[this.field] = value.value;
        this.formData[this.fieldName] = value.name;
      }
      this.showSelectPicker = false;
    },

    onConfirm(val, field) {
      let year = val.getFullYear();
      let month = val.getMonth() + 1;
      let day = val.getDate();
      let hour = val.getHours();
      let minute = val.getMinutes();
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`;
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`;
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[field] = `${year}-${month}-${day}`;
      this.showTimerPicker = false;
    },

    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      } else if (type === "day") {
        return `${value}日`;
      } else if (type === "hour") {
        return `${value}时`;
      } else if (type === "minute") {
        return `${value}分`;
      } else if (type === "second") {
        return `${value}秒`;
      }
      return value;
    },
    // 时间选择
    handleSelectTime(field) {
      this.timeField = field;
      this.showTimerPicker = true;
    },
    onTimeConfirm(val) {
      let year = val.getFullYear();
      let month = val.getMonth() + 1;
      let day = val.getDate();
      let hour = val.getHours();
      let minute = val.getMinutes();
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`;
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`;
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[this.timeField] = `${year}-${month}-${day} `;
      this.showTimerPicker = false;
    },

    formOnSubmit(type) {
      console.log(this.formData);
      this.$refs.form
        .validate()
        .then(() => {
          sellManageApi
            .secondHandCarFacade({
              ...this.formData,
              method: "updateStockOutById", //固定值
              saveOrSubmit: type == 1 ? "save" : "submit",
            })
            .then((res) => {
              console.log(res);
              if (res.code == 1) {
                if (type == 1) {
                  Notify({ type: "success", message: "保存成功" });
                } else {
                  Notify({ type: "success", message: "提交成功" });
                  this.$router.go(-1);
                }
              }
            });
          this.loading = true;
        })
        .catch(() => {
          this.loading = false;
          this.$toast.fail("请完善信息");
        });
    },
  },
};
</script>

<style lang="less" scoped>
body {
  background: #f3f3f3;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  overflow: hidden;
  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;
    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;
        .carStatus {
          border: 1px solid #35bc04;
          color: #35bc04;
          border-radius: 5px;
          padding: 0.1rem 0.1rem;
          font-size: 0.24rem;
          text-align: center;
          margin-left: 0.1rem;
        }
      }
      .hank {
        font-size: 0.28rem;
        color: #666;
        .name {
          border-right: 2px solid #ccc;
          padding-right: 0.1rem;
          margin-right: 0.1rem;
        }
      }
    }
    .detail_header_rt {
      justify-content: flex-start;
      .txt {
        font-size: 0.28rem;
        color: #161616;
      }
      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }
  .formData {
    background: #fff;
    overflow: hidden;
  }
}

.footerBtn {
  display: flex;
  justify-content: space-between;
  width: 80%;
  margin: auto;
  padding-bottom: 1rem;
  background: #fff;
  margin-top: 0.5rem;
  .saveBtn {
    background: #ccc;
    color: #fff;
    width: 45%;
    margin: auto;
    text-align: center;
    padding: 0.25rem;
    border-radius: 5px;
    font-size: 0.3rem;
    margin-right: 0.2rem;
  }
  .submitBtn {
    background: #409eff;
    color: #fff;
    width: 45%;
    margin: auto;
    text-align: center;
    padding: 0.25rem;
    border-radius: 5px;
    font-size: 0.3rem;
  }
}
.car_title {
  padding: 0.2rem;
  font-size: 0.3rem;
  font-weight: bold;
  color: #333;
}
.carInfo {
  padding: 0.2rem;
  background: #fff;
  margin-bottom: 0.2rem;
  .source {
    padding-left: 0 !important;
    margin-bottom: 0.2rem;
    justify-content: space-between;

    .source_txt {
      font-size: 0.32rem;
    }
  }

  .carInfo_No {
    margin-bottom: 0.3rem;
    .carInfo_No_item {
      border-right: 1px solid #ccc;
      padding-right: 0.1rem;
      margin-right: 0.1rem;
      font-size: 0.32rem;
      color: #333;
      &:last-child {
        border: none;
      }
    }
  }
  .carInfo_item {
    font-size: 0.32rem;
    margin-bottom: 0.1rem;
    color: #333;
    .carInfo_item_price {
      width: 50%;
    }
  }
  .carInfo_title {
    font-size: 0.32rem;
    color: #333;
    margin-bottom: 0.2rem;
    font-weight: bold;
  }
}
.checkbox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .van-checkbox__label--disabled {
    color: #333;
  }
  /deep/ .van-checkbox__icon--disabled .van-icon {
    background: none;
  }
  .check_item {
    font-size: 0.26rem;
    color: #333;
    width: 33.33%;
  }
}
</style>
